package org.leetcode.middle.leetcode739;

import java.util.Stack;

public class Solution {


    public int[] dailyTemperatures3(int[] temperatures){
        int [] res =new int[temperatures.length];

        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < temperatures.length; i++) {


            //当前元素大于栈顶对应的元素时，说明找到下一个更高的元素。通过循环不断的弹栈，直到当前的栈顶对应元素大于当前的元素
            while (!stack.isEmpty() && temperatures[i]>temperatures[stack.peek()]){
                Integer index = stack.pop();

                res[index]=i-index;
            }

            stack.push(i);
        }


        return res;

    }

        public int[] dailyTemperatures2(int[] temperatures){
        int [] res = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < temperatures.length; i++) {

            while (!stack.isEmpty() && temperatures[i]>temperatures[stack.peek()]){
                Integer index = stack.pop();

                res[index]=i-index;
            }
            stack.push(i);
        }

        return res;

    }
    //超时
    public int[] dailyTemperatures(int[] temperatures) {

        int [] res = new int[temperatures.length];

        for (int i = 0; i < temperatures.length; i++) {
            for (int j = i+1; j <temperatures.length; j++) {
                if (temperatures[i]<temperatures[j]){
                    res[i]=j-i;
                    break;
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//
//        int [] temp ={73,74,75,71,69,72,76,73};
//
//        int[] temps = solution.dailyTemperatures(temp);
//
//        for (int num :temps) {
//            System.out.println(num);
//        }


        Stack<Integer> stack = new Stack<>();

        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.peek());
    }
}
